import { _decorator, Canvas, Component, director, Director, find, instantiate, log, Node, UITransform} from 'cc';
import { GlobalUIManager } from './GlobalUIManager';

const { ccclass,property } = _decorator;

@ccclass('GlobalRoot')
export class GlobalRoot extends Component {
    _uiManager:Node =null
    public static instance: GlobalRoot;

    curUiManager: GlobalUIManager = null;
    onLoad() {
        this._uiManager = this.node.getChildByName("uiManager")
        director.addPersistRootNode(this.node);
        director.on(Director.EVENT_AFTER_SCENE_LAUNCH, () => {
            this.toScense()
        });
        GlobalRoot.instance = this

    }


    get UiManager() {
        return this.curUiManager.getComponent(GlobalUIManager)
    }
    set UiManager(n:GlobalUIManager) {
        this.curUiManager = n
    }

    

    toScense() {
        const Canvas = find("Canvas")
        if (!Canvas.getChildByName("uiManager")) {
            let cur_uiManager = instantiate(this._uiManager)
            cur_uiManager.parent = Canvas
            cur_uiManager.name = "uiManager"
            cur_uiManager.setSiblingIndex(999)
            cur_uiManager.getComponent(UITransform).width = Canvas.getComponent(UITransform).width
            cur_uiManager.getComponent(UITransform).height = Canvas.getComponent(UITransform).height
            cur_uiManager.getComponent(GlobalUIManager).init()
            this.UiManager =  cur_uiManager.getComponent(GlobalUIManager)
        }

    }

}